import java.util.Arrays;

public class L151R {
    public static void main(String[] args) {
        String str = "  hello world  ";
        String s = new Solution().reverseWords(str);
        System.out.println(s);

    }

    static class Solution {
        public String reverseWords(String s) {
            char[] chars = s.toCharArray();
            int slow = 0;
            for (int fast = 0; fast < chars.length; fast++) {
                if (fast > 0 && chars[fast - 1] != ' ' || chars[fast] != ' ') {
                    chars[slow] = chars[fast];
                    slow++;
                }
            }
            while (slow > 0) {
                if (chars[slow - 1] == ' ') {
                    slow--;
                }
                break;
            }

            int length = slow - 1;
            reverse(chars, 0, length);

            int left = 0;
            for (int i = 0; i <= length; i++) {
                if (chars[i] == ' ') {
                    reverse(chars, left, i - 1);
                    left = i + 1;
                }
            }
            if (left < length) {
                reverse(chars, left, length);
            }

            return new String(chars, 0, slow);

        }

        private void reverse(char[] charArray, int i, int j) {
            while (i < j) {
                char tmp = charArray[i];
                charArray[i] = charArray[j];
                charArray[j] = tmp;
                i++;
                j--;
            }
        }
    }
}
